const express=require("express");
const handleDB=require("../db/handleDB");
const common=require("../utils/common");
const constant=require("../utils/constant");
const md5=require("md5");
const Captcha=require("../utils/captcha");
const keys = require("../utils/keys");
const jwt=require("jsonwebtoken");
require("../utils/filter");

const router=express.Router();

router.get("/",(req,res)=>{
    (async function(){
        let userInfo=await common.getUser(req,res);

        let classify=await handleDB(res,"classify","find","数据库查询出错");

        for(var i=0;i<classify.length;i++){
            classify[i].classify_url=classify[i].classify_url?(constant.QINIU_AVATAR_URL_PRE+classify[i].classify_url):"/news/images/login.jpg"
        };

        let number="";
        if(userInfo[0]){
            let indentCounts=await handleDB(res,"indent","sql","数据库查询出错",`select count(*) from indent where user_id=${userInfo[0].user_id} and is_order=0`);
            number=indentCounts[0]["count(*)"];
        }

        let salesOrder=await handleDB(res,"commodity","find","查询数据库出错","1 order by sales_volume desc limit 6")
        let data={
            user_info:userInfo[0]?{
                nick_name:userInfo[0].nick_name,
                head_url:userInfo[0].head_url?userInfo[0].head_url:"/images/worm.jpg"
            }:false,
            classify:classify,
            number:number,
            salesOrder:salesOrder
        };
        res.render("./web_index",data);
    })();
})

router.get("/commoditys_list/",(req,res)=>{
    (async function(){
        let {sid=1,page=1,per_page=5,classify_id=1,seahname=1}=req.query;

        // console.log(seahname)
        let wh;
        if(sid==1){
            wh="upload_time";
        }
        else if(sid==2){
            wh="unit_price";
        }
        else{
            wh="sales_volume";
        }

        let result=[];

        if(seahname==1){
            if(classify_id==1){
                result=await handleDB(res,"commodity","limit","数据库查询出错",{
                where:`1 order by ${wh} desc`,
                number:page,
                count:per_page
                });
            }
            else{
                result=await handleDB(res,"commodity","limit","数据库查询出错",{
                where:`classify_id=${classify_id} order by ${wh} desc`,
                number:page,
                count:per_page
                });
            }
        }
        else{
            result=await handleDB(res,"commodity","limit","数据库查询出错",{
            where:`commodity_name like "%${seahname}%" order by ${wh} desc`,
            number:page,
            count:per_page
            });
        }

        let result2=await handleDB(res,"commodity","sql","数据库查询出错","select count(*) from commodity where 1")
        
        let totalPage=Math.ceil(result2[0]['count(*)']/per_page)

        res.send({
            commodityList:result,
            totalPage,
            currentPage:Number(page)
        })
    })();
})

module.exports=router